package org.reliablesource.pleiades;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class BrowserPlugin extends AbstractUIPlugin {
	private static BrowserPlugin DEFAULT;

	// given a resource id, get a resource object
	private final Map<String, ReliableResource> resourceMap = new HashMap<String, ReliableResource>();

	// given a resource, get comment List
	private final Map<String, List<ReliableSimpleComment>> resourceCommentListMap = new HashMap<String, List<ReliableSimpleComment>>();

	// given a User, get resource List.
	private final Map<String, List<ReliableResource>> userResourceMap = new HashMap<String, List<ReliableResource>>();

	// given User id, get a User object.
	private final Map<String, ReliableUser> userMap = new HashMap<String, ReliableUser>();

	public BrowserPlugin() {
		DEFAULT = this;
	}

	public static BrowserPlugin getDefault() {
		return DEFAULT;
	}

	/**
	 * Logs the given throwable.
	 * 
	 * @param t
	 *            the throwable to log
	 */
	public void log(Throwable t) {
		String msg = t.getMessage();
		if (msg == null)
			msg = t.toString();
		IStatus status = new Status(IStatus.ERROR, getBundle()
				.getSymbolicName(), 0, msg, t);
		getLog().log(status);
	}

	/**
	 * Returns a list of all views and editors in the given page, excluding any
	 * secondary views like the History view.
	 * 
	 * @param page
	 *            the workbench page
	 * @return a list of all non-secondary parts in the page
	 */
	public static List getNonSecondaryParts(IWorkbenchPage page) {
		ArrayList list = new ArrayList();
		list.addAll(Arrays.asList(page.getViewReferences()));
		list.addAll(Arrays.asList(page.getEditorReferences()));
		for (Iterator i = list.iterator(); i.hasNext();) {
			IWorkbenchPartReference ref = (IWorkbenchPartReference) i.next();
			if (ref instanceof ISecondaryPart) {
				i.remove();
			}
		}
		return list;
	}

	public synchronized ReliableUser getUser(String _userId) {
		if (!userMap.containsKey(_userId)) {
			ReliableUser _user = new ReliableUser();
			_user.setId(_userId);
			userMap.put(_userId, _user);
		}

		return userMap.get(_userId);
	}

	public synchronized ReliableResource getResourceLink(String _url) {
		if (!resourceMap.containsKey(_url)) {
			ReliableResource _resource = new ReliableResource();
			_resource.setId(_url);
			resourceMap.put(_url, _resource);
		}

		return resourceMap.get(_url);
	}

	public ReliableSimpleComment[] getResourceCommentList(String _url) {
		if (!resourceMap.containsKey(_url)) {
			ReliableResource _resource = new ReliableResource();
			_resource.setId(_url);
			resourceMap.put(_url, _resource);
		}

		ReliableResource _RsrcKey = resourceMap.get(_url);
		String _key = _RsrcKey.getId();

		if (!resourceCommentListMap.containsKey(_key)) {
			List<ReliableSimpleComment> _comments = new LinkedList<ReliableSimpleComment>();
			resourceCommentListMap.put(_key, _comments);
		}

		List<ReliableSimpleComment> _comments = resourceCommentListMap
				.get(_key);

		return _comments.toArray(new ReliableSimpleComment[_comments.size()]);
	}
}
